package com.zhang.novel.controller;

import com.zhang.novel.core.cache.CacheService;
import com.zhang.novel.core.utils.RandomValidateCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@Api(tags = "文件接口")
@RequestMapping("file")
@RequiredArgsConstructor
@Slf4j
public class FileController extends BaseController {

    private final CacheService cacheService;

    /**
     * 获取验证码
     * @param response
     */
    @GetMapping("getVerify")
    @ApiOperation("获取验证码")
    public void getVerify(HttpServletResponse response){
        try {
            // 设置响应类型
            response.setContentType("image/jpeg");

            // 设置响应头信息，告知浏览器不用缓存验证码
            response.setHeader("Pragma","No-cache");
            response.setHeader("Cache-Control","no-cache");
            response.setDateHeader("Expire",0);

            // 向页面输出验证码
            RandomValidateCodeUtil randomValidateCodeUtil = new RandomValidateCodeUtil();
            randomValidateCodeUtil.getRandcode(cacheService,response);
        }catch (Exception e){
            log.error("获取验证码失败>>>> ", e);
        }
    }
}
